package com.leet.twopoint;

import java.util.HashSet;
import java.util.Set;

public class IsCircle {
    public ListNode detectCycle(ListNode head) {
        ListNode pos = head;
        Set<ListNode> visited = new HashSet<ListNode>();
        while(pos!=null){
            if(visited.contains(pos)) return pos;
            visited.add(pos);
            pos = pos.next;
        }
        return pos;
    }
}
